package com.test.ws.s1.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.ws.s1.bean.Acl_user;
import com.test.ws.s1.bean.Imgs;
import com.test.ws.s1.bean.Route;
import com.test.ws.s1.service.ImagsService;
import com.test.ws.s1.utils.Res;
import com.test.ws.s1.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.UUID;

@Controller
public class ImgsController {
    @Autowired
    private ImagsService imagsService;

    /// 查询附图
    @RequestMapping("/imgsCha")
    public String selectImgs(Imgs imgs, HttpServletRequest request,
                            @RequestParam(defaultValue = "1") int pageNum,
                            @RequestParam(defaultValue = "5") int pageSize) {
        String id = request.getParameter("id");
        String userName = request.getParameter("user_name");

        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(pageNum,pageSize);
        ArrayList<Imgs> imgsList = imagsService.selectImgs(id,userName);
        PageInfo pageInfo = new PageInfo(imgsList);

        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("imgsList", imgsList);
        return "houtai/futu";
    }

    /// 添加附图
    @RequestMapping("/imgsAdd")
    public String insertImgs(Imgs users, HttpServletRequest request, MultipartFile[] image) throws IOException {

        String id = request.getParameter("route_id");
        if (image != null && image.length > 0) {
            for (MultipartFile file : image) {
                if (!file.isEmpty()) {
                    String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                    String originalName = file.getOriginalFilename();
                    String extension = originalName.substring(originalName.lastIndexOf(".") + 1);
                    String fileName = uuid + "." + extension;

                    file.transferTo(new File(Res.fileDir + fileName));
                    users.setImgs_dz(fileName); // 需要根据实际字段调整
                    int result = imagsService.insertImgs(id,users);
                }
            }
        }

        return "redirect:/imgsCha?id=" + id;
    }

    /// 进入附图jsp
    @RequestMapping("/routeFuTu")
    public String routeFuTu(Route route,HttpServletRequest request) {
        String id = request.getParameter("id");
        request.getSession().setAttribute("rid",route);
        return "houtai/futuAdd";
    }


    /// 回显
    @RequestMapping("/imgsGai")
    public String updateImgs(Imgs imgs, HttpServletRequest request) {
        String id = request.getParameter("id");
        Imgs imgs1 = imagsService.selectImgsById(id);

        request.getSession().setAttribute("im",imgs1);
        return "houtai/futuXiuGai";
    }

    /// 修改附图
    @RequestMapping("/imgGai")
    public String updateUsers(MultipartFile image, Model model,HttpServletRequest request,Imgs user) throws SQLException, IOException {
        String id = request.getParameter("imgs_id");
        System.out.println(image);
        if (image.getSize() > 0) {
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            String houZhui = image.getOriginalFilename().split("\\.")[1];
            ///新增加的头像路径
            String path = Res.fileDir + uuid + "." + houZhui;
            image.transferTo(new File(path));
            user.setImgs_dz(uuid + "." + houZhui);
            /// 获取旧头像
            Imgs imgs1 = imagsService.selectImgsById(id);
            String tx = imgs1.getImgs_dz();

            if (tx != null && !tx.equals(user.getImgs_dz())) {
                File file = new File(Res.fileDir + tx);
                if (file.exists()) {
                    file.delete();
                }
            }
        }
        int i = imagsService.updateImgs(id, user);
        if (i > 0) {
            return "redirect:/imgsCha";

        }
        return "redirect:/imgsCha";

    }

    @RequestMapping("/imgsDel")
    public String deleteImgs(Imgs imgs,HttpServletRequest request) {
        String id = request.getParameter("id");
        int i = imagsService.deleteImgs(id);
        if (i > 0) {
            return "redirect:/imgsCha";
        }
        return "redirect:/imgsCha";
    }


    /// 查询轮播图
    @RequestMapping("/imgsLunBo")
    public String selectImgsLunBo(Imgs imgs, HttpServletRequest request) {
        ArrayList<Imgs> imgsList = imagsService.selectImgsLunBo();
        request.setAttribute("imgLunBo", imgsList);
        return "index";
    }
}
